/**
 * Created by mengwei666 on 2016/12/8.
 */
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
public class Email {
    private static final String host="smtp.sina.cn";
    private static final String home_address="13026135129m@sina.cn";
    private static final String password="storm52314";

    public static void sendMail(String target_address,String content){
        Properties props=System.getProperties();
        props.put("mail.smtp.host",host);
        props.put("mail.smtp.auth","true");
        Session session=Session.getDefaultInstance(props, new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(home_address,password);
            }
        });
        try{
            MimeMessage message=new MimeMessage(session);
            message.setFrom(new InternetAddress(home_address));
            message.addRecipient(Message.RecipientType.TO,new InternetAddress(target_address));
            message.setSubject("java");
            message.setText(content);
            Transport.send(message);
        }
        catch(javax.mail.MessagingException e){
            e.printStackTrace();
        }
    }

    public static void getMail() throws Exception{
        Properties props=new Properties();
        //设置邮件接收协议为smtp
        props.setProperty("mail.store.protocol", "pop3");
        props.setProperty("mail.pop3.host", "pop3.sina.cn");

        Session session = Session.getInstance(props);
        Store store = session.getStore("pop3");
        //连接要获取数据的邮箱 主机+用户名+密码
        store.connect("pop3.sina.cn", home_address, password);
        Folder folder = store.getFolder("inbox");
        //邮件可读可写
        folder.open(Folder.READ_WRITE);

        Message[] messages = folder.getMessages();

        for (int i = 0; i < messages.length; i++) {
            //发件人地址
            String address = messages[i].getFrom()[0].toString();
            //邮件主题
            String subject = messages[i].getSubject();
            //如果只是纯文本文件
            //String content = (String) messages[i].getContent();
            //MIME中包含文本
            getTextMultipart(messages[i],subject,address);
            //MIME中包含图片
            getPicMultipart(messages[i]);
            //MIME中包含附件
            //getAttachmentMultipart(messages[i]);
            //解析所有类型
            //getAllMultipart(messages[i]);
        }
        folder.close(true);
        store.close();
    }

    private static void getPicMultipart(Part part) throws Exception{
        if(part.isMimeType("multipart/*")){
            Multipart multipart = (Multipart) part.getContent();
            int count = multipart.getCount();
            for (int i = 0; i < count; i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                if(bodyPart.isMimeType("image/*")){
                    InputStream is = bodyPart.getInputStream();
                    FileOutputStream fos=new FileOutputStream("1.jpg");
                    int len=0;
                    byte[] bys=new byte[1024];
                    while((len=is.read(bys))!=-1){
                        fos.write(bys, 0, len);
                    }
                    fos.close();
                }
            }
        }
    }

    private static void getTextMultipart(Part part,String subject,String address) throws Exception{
        if(subject.contains("java")) {
            if (part.isMimeType("text/html")) {
                String content = (String) part.getContent();
                System.out.println(content);
                if(content.contains("A") || content.contains("Yes") || content.contains("yes")){
                    Email.sendMail(address,"Thank you!");
                }
                else if(content.contains("B") || content.contains("No") || content.contains("no")){
                    Email.sendMail(address,"Oh My God!");
                }
                else{
                    Email.sendMail(address,"Can't understand!");
                }
            } else if (part.isMimeType("text/plain")) {
                String content = (String) part.getContent();
                System.out.println(content);
                if(content.contains("A") || content.contains("Yes") || content.contains("yes")){
                    Email.sendMail(address,"Thank you!");
                }
                else if(content.contains("B") || content.contains("No") || content.contains("no")){
                    Email.sendMail(address,"Oh My God!");
                }
                else{
                    Email.sendMail(address,"Can't understand!");
                }
            }
        }
    }


    public static void main(String[] args) throws Exception{
        DBOpener operator =new DBOpener();
        operator.sendAll("Do you like me? A:Yes B:No");
        Email.getMail();
    }
}
